package de.ghost23.stubcodegenerator;

import java.io.IOException;
import org.antlr.runtime.*;

import de.ghost23.stubcodegenerator.recognize.*;
import de.ghost23.util.ParameterAnalyzer;

public class StubCodeGenerator {

	private static CreateStubCodeAndFolders creator;
	private static ParameterAnalyzer parameters;
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		if(args.length < 1) {
			printHelp();
			return;
		}
		
		parameters = new ParameterAnalyzer(args);
		
		stubcodegrammarLexer lex = null;
		System.out.println("StubCodeGenerator, v1.0, by Sven Busse ...");
		System.out.println("Reading: " + parameters.getStandaloneValues()[0]);
		
		try {
			lex = new stubcodegrammarLexer(new ANTLRFileStream(parameters.getStandaloneValues()[0]));
		}catch (IOException e) {
			System.err.println("Error: loading file failed.");
		}
		CommonTokenStream tokens = new CommonTokenStream(lex);

		stubcodegrammarParser g = new stubcodegrammarParser(tokens);
		try {
			if(parameters.getConfigurators().containsKey("t")) {
				creator = new CreateStubCodeAndFolders(g.prog(), parameters.getConfigurators().get("o"), parameters.getConfigurators().get("t"));
			}else {
				creator = new CreateStubCodeAndFolders(g.prog(), parameters.getConfigurators().get("o"));
			}
		} catch (RecognitionException e) {
			e.printStackTrace();
		}
		creator.execute();
		System.out.println("Done! Look here for the result: \n" + parameters.getConfigurators().get("o"));
	}
	
	private static void printHelp() {
		
		System.out.println("StubCodeGenerator, v1.0, by Sven Busse, HELP:");
		System.out.println("use: java StubCodeGenerator [options] \\path\\to\\stubcodestructurefile.txt");
		System.out.println("For help on what this is all about, visit: www.ghost23.de/blog.");
		System.out.println("[options]:");
		System.out.println("-o\t: output directory. Where to put all the generated stuff");
		System.out.println("-t\t: optional, a directory with alternative templates.");
	}
}
